<?php
// JWT Token测试脚本
require_once __DIR__ . '/app/Http/keys.php';

$token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoxLCJ0aW1lc3RhbXAiOjE3NTU1MDcwNjZ9.62f419525a010cd2612bb6be8b486ebb08ee2068bc1dbd21585aa4c259a54f7f';

echo "测试JWT Token解析\n";
echo "Token: $token\n\n";

// 分解token
$jwtParts = explode('.', $token);
echo "JWT部分数量: " . count($jwtParts) . "\n";

if (count($jwtParts) !== 3) {
    echo "错误: JWT格式不正确\n";
    exit;
}

[$header, $payload, $signature] = $jwtParts;

echo "Header: $header\n";
echo "Payload: $payload\n";
echo "Signature: $signature\n\n";

// 解码header
$headerData = json_decode(base64_decode($header), true);
echo "Header解码: " . json_encode($headerData) . "\n";

// 解码payload (处理URL安全编码)
$payload_processed = str_replace(['-', '_'], ['+', '/'], $payload);
$payload_processed = str_pad($payload_processed, strlen($payload_processed) % 4, '=', STR_PAD_RIGHT);

echo "Payload处理前: $payload\n";
echo "Payload处理后: $payload_processed\n";

$payloadData = json_decode(base64_decode($payload_processed), true);
echo "Payload解码: " . json_encode($payloadData) . "\n\n";

// 验证签名
$validSignature = hash_hmac('sha256', "$header.$payload", TOKEN_KEY);
echo "原始签名: $signature\n";
echo "计算签名: $validSignature\n";
echo "签名匹配: " . ($signature === $validSignature ? '是' : '否') . "\n";

if (isset($payloadData['user_id'])) {
    echo "用户ID: " . $payloadData['user_id'] . "\n";
} else {
    echo "错误: 无法获取用户ID\n";
}